package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.page.PageBean;
import com.dongdongshop.util.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.vo.SpecVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("spec")
public class SpecController {

    @DubboReference
    private SpecificationService ss;

    @RequestMapping("show")
    public String show(){
        return "admin/specification";
    }


    @RequestMapping("getAll")
    @ResponseBody
    public Result getAll(@RequestParam(defaultValue = "1")Integer pageNum,
                         @RequestParam(defaultValue = "5")Integer pageSize, String name){
        try {
            PageBean<TbSpecification> all = ss.getAll(pageNum, pageSize, name);
            return Result.OK().setData(all);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR);
        }
    }

    @RequestMapping("getSelect")
    @ResponseBody
    public Result getSelect(){
        try {
            List<TbSpecification> all = ss.getAll1();
            return Result.OK().setData(all);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR);
        }
    }


    @RequestMapping("insert")
    @ResponseBody
    public Integer insert(TbSpecification specification,String arrStr){
        List<TbSpecificationOption> list = JSONObject.parseArray(arrStr, TbSpecificationOption.class);
        return ss.saveSpecAndOptions(specification,list);
    }

    //回显
    @RequestMapping("toupdate")
    @ResponseBody
    public SpecVO toUpdate(Long id){
        return ss.toupdate(id);
    }

    //修改
    @RequestMapping("update")
    @ResponseBody
    public Integer update(SpecVO specVO,String arrStr){
        List<TbSpecificationOption> options = JSONObject.parseArray(arrStr, TbSpecificationOption.class);
        specVO.setSpecOptionsList(options);
        return ss.update(specVO);
    }


    @RequestMapping("deleteCheck")
    @ResponseBody
    public int deletecCheck(Long[] ids){
        return ss.deleteCheck(ids);
    }

}
